package com.houg.testuserapi.controller;

import com.houg.testuserapi.common.constants.Base;
import com.houg.testuserapi.common.constants.ResultCode;
import com.houg.testuserapi.common.resp.Resp;
import com.houg.testuserapi.common.utils.Md5Utils;
import com.houg.testuserapi.model.dto.UserLoginDto;
import com.houg.testuserapi.model.entity.User;
import com.houg.testuserapi.model.vo.UserVo;
import com.houg.testuserapi.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

/**
 * @Author: NALHOUG
 * @Time: 2019/11/8 15:33
 * @Explain: 用户控制层
 */
@RestController
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户登录
     * @return resp 返回对象
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Resp userLogin(UserLoginDto userLoginDto,HttpServletRequest request){
        Resp resp = new Resp();
        loginOption(userLoginDto,resp,request);
        return resp;
    }

    /**
     * 注册账户
     * @return resp
     */
    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Resp register(UserLoginDto userLoginDto){
        Resp resp = new Resp();
        if (null!=userService.selectByUserId(userLoginDto.getUserId())){
            resp.setResultCode(ResultCode.ACCOUNT_HAS_USERNAME);
            return resp;
        }
        if (0<userService.insert(userLoginDto)){
            resp.setResultCode(ResultCode.ACCOUNT_REGISTER_SUCCESS);
            return resp;
        }
        resp.setResultCode(ResultCode.ACCOUNT_REGISTER_ERROR);
        return new Resp();
    }
    @ResponseBody
    @RequestMapping(value = "/checkId={userId}",method = RequestMethod.POST)
    public Resp checkId(@PathVariable String userId){
        Resp resp = new Resp();
        if (null!=userService.selectByUserId(userId)){
            resp.setResultCode(ResultCode.ACCOUNT_HAS_USERNAME);
            return resp;
        }
        resp.setResultCode(ResultCode.SUCCESS);
        return resp;
    }

    /**
     * 退出登录
     * @return resp
     */
    @ResponseBody
    @RequestMapping(value = "/exit", method = RequestMethod.POST)
    public Resp userExit(HttpServletRequest request){
        Resp resp = new Resp();
        request.getSession().removeAttribute(Base.THIS_USER);
        resp.setResultCode(ResultCode.LOGOUT_SUCCESS);
        return resp;
    }

    /**
     * 查看资料
     * @return resp
     */
    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public Resp userInfo(String userId){
        User user = userService.selectByUserId(userId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        return Resp.success(userVo);
    }

    /**
     * 更新用户资料
     * @param userVo 前端对象
     * @return 更新结果
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Resp updateUser(UserVo userVo){

        if (0<userService.updateUser(userVo)){
            User user = userService.selectByUserId(userVo.getUserId());
            BeanUtils.copyProperties(user,userVo);
            return Resp.success(userVo);
        }
        return Resp.error(ResultCode.ERROR);
    }



    /**
     * 获取Session中的用户
     * @param request SessionID
     * @return request Session用户
     */
    private User thisAccount(HttpServletRequest request){
        return (User)request.getSession().getAttribute(Base.THIS_USER);
    }

    /**
     * 登录方法
     * @param user dto用户
     * @param resp 返回对象
     * @param request 请求
     */
    private void loginOption(UserLoginDto user, Resp resp, HttpServletRequest request){
        User dbUser = null;
        String userId = user.getUserId();
        String md5Password = Md5Utils.encrypt3Times(user.getPassword());
        dbUser = userService.selectByUserId(userId);
        //判断用户是否存在
        if (null==dbUser){
            resp.setResultCode(ResultCode.ACCOUNT_NOT_EXIST);
            return;
        }
        //判断密码是否正确
        if (!md5Password.equals(dbUser.getPassword())){
            resp.setResultCode(ResultCode.LOGIN_PASSWORD_ERROR);
            System.out.println(user.getPassword()+dbUser.getPassword());
            System.out.println(md5Password);
            return;
        }

        HttpSession session = request.getSession();
        //状态正常，存入session
        session.setAttribute(Base.ROLE_USER, dbUser);
        resp.setResultCode(ResultCode.LOGIN_SUCCESS);

    }

}
